/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ens.delegation.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.ens.delegation.bo.Agent;
import org.ens.delegation.dao.IAgentDao;

/**
 *
 * @author amine
 */
public class AgentAction extends GenericAction{

    private Agent agent;
    private List listAgentPag;
    
    private String[] columnsSort = { "", "nomFR", "nomAR","cin",
            "dateNaiss", "dateRec", "dateNom", "dateTit","fonction", "etablissement","service" };
    
    private Map _jsonPaginator;
    private Map jsonResult;
    private Map jsonData;
       
    private String start;
    private String length;
    private String sort;
    private String sortdir;
    private String echo;

    private String strNumDoti;
    private String strNomFr;
    private String strNomAr;
    private String strCin;
    private String strDateNaiss;
    private String strDateTit;
    private String strEtablissement;

    private IAgentDao agentDao;
    
    public AgentAction() {
    }

    public AgentAction(IAgentDao agentDao) {
        this.agentDao = agentDao;
    }
    
    @Override
    public String execute() throws Exception {
        setAgent(null);
        setActionMessages(null);
        System.out.println("EXECUTE ...");
        return SUCCESS;
    }
    
    @Override
    public String searchPaginator(){
        
        setJsonPaginator(new HashMap<String,Object>());
        
        setListAgentPag(getAgentDao().findByFiltersWithFirstMaxResult(Integer.valueOf(getStart()), Integer.valueOf(getLength()), getColumnsSort()[Integer.valueOf(getSort())], getSortdir(), getStrNumDoti(), getStrCin(), getStrNomFr(), getStrNomAr(), null, null, getStrEtablissement()));
                
        getJsonPaginator().put("sEcho", getEcho());
        
        Integer rowCount = getAgentDao().getRowCountByFilters(getStrNumDoti(), getStrCin(), getStrNomFr(), getStrNomAr(), null, null, getStrEtablissement());
        
        getJsonPaginator().put("iTotalRecords", String.valueOf(rowCount));
        
        if (Integer.valueOf(getLength()).compareTo(rowCount) > 0) {
            getJsonPaginator().put("iTotalDisplayRecords", getLength());
        } else {
            getJsonPaginator()
                    .put("iTotalDisplayRecords", String.valueOf(rowCount));
        }
        
        String[][] aaData = new String[getListAgentPag().size()][];
        for (int i = 0; i < getListAgentPag().size(); i++) {
            Agent ag = (Agent) getListAgentPag().get(i);
            String s [] = new String[7];
            s[0] = "<input type=\"checkbox\" name=" + ag.getId()
                    + "></input>";
            
            s[1] = ag.getId().toString();
            s[2] = ag.getCin();
            s[3] = ag.getNomFr();
            s[4] = ag.getNomAr();
            s[5] = ag.getEtablissementAffecte().getNomFr();
            s[6] = "<a href=\"getAgentsMod.go?ids=" + ag.getId()
                    + "\"><img src=\"./gfx/icon-edit.png\" alt=\"edit\" /></a>";
            
            aaData[i] = s;
        }
        
        getJsonPaginator().put("aaData", aaData);
        return SUCCESS;
    }

    @Override
    public String add() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String edit() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String save() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String saveNew() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String delete() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    /**
     * @return the agent
     */
    public Agent getAgent() {
        return agent;
    }

    /**
     * @param agent the agent to set
     */
    public void setAgent(Agent agent) {
        this.agent = agent;
    }

    /**
     * @return the _jsonPaginator
     */
    public Map get_jsonPaginator() {
        return getJsonPaginator();
    }

    /**
     * @param jsonPaginator the _jsonPaginator to set
     */
    public void set_jsonPaginator(Map jsonPaginator) {
        this.setJsonPaginator(jsonPaginator);
    }

    /**
     * @return the listAgentPag
     */
    public List getListAgentPag() {
        return listAgentPag;
    }

    /**
     * @param listAgentPag the listAgentPag to set
     */
    public void setListAgentPag(List listAgentPag) {
        this.listAgentPag = listAgentPag;
    }

    /**
     * @return the _jsonPaginator
     */
    public Map getJsonPaginator() {
        return _jsonPaginator;
    }

    /**
     * @param jsonPaginator the _jsonPaginator to set
     */
    public void setJsonPaginator(Map jsonPaginator) {
        this._jsonPaginator = jsonPaginator;
    }

    /**
     * @return the jsonResult
     */
    public Map getJsonResult() {
        return jsonResult;
    }

    /**
     * @param jsonResult the jsonResult to set
     */
    public void setJsonResult(Map jsonResult) {
        this.jsonResult = jsonResult;
    }

    /**
     * @return the jsonData
     */
    public Map getJsonData() {
        return jsonData;
    }

    /**
     * @param jsonData the jsonData to set
     */
    public void setJsonData(Map jsonData) {
        this.jsonData = jsonData;
    }

    /**
     * @return the start
     */
    public String getStart() {
        return start;
    }

    /**
     * @param start the start to set
     */
    public void setStart(String start) {
        this.start = start;
    }

    /**
     * @return the length
     */
    public String getLength() {
        return length;
    }

    /**
     * @param length the length to set
     */
    public void setLength(String length) {
        this.length = length;
    }

    /**
     * @return the sort
     */
    public String getSort() {
        return sort;
    }

    /**
     * @param sort the sort to set
     */
    public void setSort(String sort) {
        this.sort = sort;
    }

    /**
     * @return the sortdir
     */
    public String getSortdir() {
        return sortdir;
    }

    /**
     * @param sortdir the sortdir to set
     */
    public void setSortdir(String sortdir) {
        this.sortdir = sortdir;
    }

    /**
     * @return the echo
     */
    public String getEcho() {
        return echo;
    }

    /**
     * @param echo the echo to set
     */
    public void setEcho(String echo) {
        this.echo = echo;
    } 
    
    /**
     * @return the strNomFr
     */
    public String getStrNomFr() {
        return strNomFr;
    }

    /**
     * @param strNomFr the strNomFr to set
     */
    public void setStrNomFr(String strNomFr) {
        this.strNomFr = strNomFr;
    }

    /**
     * @return the strNomAr
     */
    public String getStrNomAr() {
        return strNomAr;
    }

    /**
     * @param strNomAr the strNomAr to set
     */
    public void setStrNomAr(String strNomAr) {
        this.strNomAr = strNomAr;
    }

    /**
     * @return the columnsSort
     */
    public String[] getColumnsSort() {
        return columnsSort;
    }

    /**
     * @param columnsSort the columnsSort to set
     */
    public void setColumnsSort(String[] columnsSort) {
        this.columnsSort = columnsSort;
    }

    /**
     * @return the strCin
     */
    public String getStrCin() {
        return strCin;
    }

    /**
     * @param strCin the strCin to set
     */
    public void setStrCin(String strCin) {
        this.strCin = strCin;
    }

    /**
     * @return the strDateNaiss
     */
    public String getStrDateNaiss() {
        return strDateNaiss;
    }

    /**
     * @param strDateNaiss the strDateNaiss to set
     */
    public void setStrDateNaiss(String strDateNaiss) {
        this.strDateNaiss = strDateNaiss;
    }

    /**
     * @return the strDateTit
     */
    public String getStrDateTit() {
        return strDateTit;
    }

    /**
     * @param strDateTit the strDateTit to set
     */
    public void setStrDateTit(String strDateTit) {
        this.strDateTit = strDateTit;
    }

    /**
     * @return the strEtablissement
     */
    public String getStrEtablissement() {
        return strEtablissement;
    }

    /**
     * @param strEtablissement the strEtablissement to set
     */
    public void setStrEtablissement(String strEtablissement) {
        this.strEtablissement = strEtablissement;
    }

    /**
     * @return the agentDao
     */
    public IAgentDao getAgentDao() {
        return agentDao;
    }

    /**
     * @param agentDao the agentDao to set
     */
    public void setAgentDao(IAgentDao agentDao) {
        this.agentDao = agentDao;
    }

    /**
     * @return the strNumDoti
     */
    public String getStrNumDoti() {
        return strNumDoti;
    }

    /**
     * @param strNumDoti the strNumDoti to set
     */
    public void setStrNumDoti(String strNumDoti) {
        this.strNumDoti = strNumDoti;
    }  
    
}
